package com.jsyl.lwbk.web.controller.education;

import com.jsyl.lwbk.business.domain.education.TrainingProgram;
import com.jsyl.lwbk.business.dto.education.TrainingProgramListDTO;
import com.jsyl.lwbk.business.dto.education.UpdateTrainingProgramListDTO;
import com.jsyl.lwbk.business.service.IQuestionService;
import com.jsyl.lwbk.business.service.education.ITrainingProgramService;
import com.jsyl.lwbk.common.annotation.Log;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.common.enums.BusinessType;
import com.jsyl.lwbk.common.utils.poi.ExcelUtil;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author jh
 * @date 2021-04-02 16:27
 * @desc 训练项目管理
 **/
@Controller
@RequestMapping("/education/trainingProgram")
public class TrainingProgramController extends BaseController {

    private String prefix = "education/trainingProgram";

    @Autowired
    private IQuestionService questionService;

    @Autowired
    private ITrainingProgramService iTrainingProgramService;



    @GetMapping("/edit/{questionId}")
    public String trainingProgram(@PathVariable("questionId") Long questionId,ModelMap mmap) {
        mmap.put("question", questionService.findById(questionId));
        return prefix + "/trainingProgram";
    }

    /**
     * 项目列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrainingProgramListDTO trainingProgramListDTO) {
        startPage();
        List<TrainingProgram> list = iTrainingProgramService.findList(trainingProgramListDTO);
        return getDataTable(list);
    }

    /**
     * 训练项目管理
     */
    @Log(title = "训练项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Valid UpdateTrainingProgramListDTO updateTrainingProgramListDTO) {
        iTrainingProgramService.update(updateTrainingProgramListDTO, ShiroUtils.getLoginName());
        return toAjax(Boolean.TRUE);
    }

    @Log(title = "训练项目管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Integer evaluateModuleType,Long questionId, Integer trainingScore) throws Exception {
        ExcelUtil<TrainingProgram> util = new ExcelUtil<TrainingProgram>(TrainingProgram.class);
        List<TrainingProgram> trainingProgramList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = iTrainingProgramService.importTrainingProgram(trainingProgramList,operName);
        return AjaxResult.success(message);
    }


    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<TrainingProgram> util = new ExcelUtil<TrainingProgram>(TrainingProgram.class);
        return util.importTemplateExcel("训练项目");
    }

}
